import {Model} from './model.js';
var model=new Model();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user_info:'',
    mobile: '',
    code: '',
    currentTime: 60,//限制60s
    isClick: false, //禁用发送验证码
    send_sms_title: '获取验证码',
    alert_mobile:true,
    modal: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    model.getUserRequest((res) => {
      this.setData(res.data);
    })
    this._getUserInfo()
    wx.setStorageSync('userInfo', true)
    model.system((res)=>{
      this.setData({system:res.data})
    })
    // 通过推荐人二维码进入
    var scene = options.scene
    if(typeof scene!='undefined'){
      model.setHCard(scene,(res)=>{
        console.log(res)
      })
    }
  },

  onShow:function(){
    wx.setStorageSync('pullUserInfo', false)
    if(!wx.getStorageSync('userInfo')){
      this._getUserInfo()
    }
  },

  // 下拉刷新
  onPullDownRefresh(){
    wx.showNavigationBarLoading()
    var pull_user_info = wx.getStorageSync('pullUserInfo');
    if (!pull_user_info){
      this._getUserInfo()
      wx.setStorageSync('pullUserInfo',true)
    }
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
  },
  
 // 获取用户信息
  _getUserInfo:function(callback){
    wx.showLoading({
      title: '加载中...',
    })
    model.getUserInfo((res) => {
      var data=res.data;
      if (res.statusCode == 404) {
        wx.setStorageSync('userInfo', false)
        wx.navigateTo({
          url: '/pages/authorize/index'
        })
      } else {
        if(data.data){
          wx.setStorageSync('userInfo', true)
          var alert_mobile = res.data.data.mobile == '' || (typeof res.data.data.mobile == 'undefined') ? true : false;
          wx.hideLoading()
          this.setData({ user_info: res.data.data, alert_mobile: alert_mobile })
        }
        callback&&callback(res)
      }
    })
  },

  // 获取手机号
  mobile: function (event) {
    this.setData({ mobile: event.detail });
  },

  // 获取验证码
  code: function (event) {
    this.setData({ code: event.detail});
  },

  // 显示验证手机号窗口
  onModal:function(event){
    this.setData({ modal:true})
  },

  onClose(){
    this.setData({ modal:false})
  },

  // 验证手机号
  checkSmsCode: function (event) {

      var code = this.data.code;
      var mobile = this.data.mobile;
      if (mobile.length <= 0) {
        wx.showToast({
          title: '请先输入手机号码',
          icon: 'none',
        });
        return;
      }

      if (code.length <= 0) {
        wx.showToast({
          title: '请输入短信验证码',
          icon: 'none',
        });
        return;
      }
      model.checkSmsCode(mobile, code, (res) => {
        
        if (res.code==1) {
          wx.showToast({
            title: res.msg,
            icon: 'success',
          });

          setTimeout(() => {
            this.setData({
              modal: false,
              mobile: '',
              code: '',
              alert_mobile:false
            });
          }, 1000);
        } 
        if(res.data.msg_code==80002){
          console.log(res)
          wx.showToast({
            title: '验证码错误~',
            icon: 'none',
          });
          this.setData({ code: '' })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          });
        }
      })
  },

  // 发送验证码
  sendsms: function (event) {
    var mobile = this.data.mobile
    let that = this;
    /*第一步：验证手机号码*/
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;// 判断手机号码的正则
    if (mobile.length == 0) {
      wx.showToast({ title: '手机号码不能为空', icon: 'none' })
      return;
    }

    if (mobile.length < 11) {
      wx.showToast({ title: '手机号码长度有误！', icon: 'none' })
      return;
    }

    if (!myreg.test(mobile)) {
      wx.showToast({ title: '手机号码格式错误！', icon: 'none' })
      return;
    }

    /*第二步：设置计时器*/
    // 先禁止获取验证码按钮的点击
    that.setData({
      isClick: true,
    })

    // 60s倒计时 setInterval功能用于循环，常常用于播放动画，或者时间显示
    var currentTime = that.data.currentTime;
    var interval = setInterval(function () {
      currentTime--;//减
      that.setData({
        send_sms_title: currentTime + '秒后重新获取'
      })
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          send_sms_title: '获取验证码',
          currentTime: 60,
          isClick: false
        })
      }
    }, 1000);

    // 发送短信验证码
    model.sendsms(mobile, (res) => {
      var data=res.data
      if (data.msg_code==80003){
        clearInterval(interval)
        that.setData({
          isClick: false,
          send_sms_title: '获取验证码'
        })
        wx.showToast({
          title: data.msg,
          icon: 'none',
        });
      }
      if(res.code==1){
        wx.showToast({
          title: res.msg,
          icon: 'none',
        });
      }
    })
  },
  //分享 
  onShare:function(){
    var user_type=this.data.user_info.type;
    if(user_type==0){
      wx.showToast({
        title: '请先成为筑梦者或追梦人才能邀请好友哦！', icon:"none"
      })
      return ;
    }
    wx.navigateTo({
      url: '/pages/share/index',
    })
  },
  // 客服电话
  clickTel:function(e){
    wx.makePhoneCall({
      phoneNumber: this.data.system.tel,
    })
  },
  // 申请筑梦者or追梦人
  onApply:function(e){
    // 身份对应值
    var user_type=new Array();
        user_type['zmz']=1;
        user_type['zmr']=2;

    if(this.data.alert_mobile){
      wx.showToast({
        title: '请先认证手机号',
        icon: 'none'
      })
      return;
    }
    var apply_type=e.currentTarget.dataset.type;
    var data_type=this.data.user_info.type
    var type_text=this.data.user_info.type_text;
    var user_applay=this.data.user_info.user_applay;
    if(user_applay.length!=0){
      if (user_applay.type != user_type[apply_type] && user_applay.status!=1) {   //判断申请身份
        wx.showToast({
          title: '请勿同时申请两种身份~', icon: "none"
        })
        return;
      }
    }
    
    if(data_type==1){  //筑梦者不能再次申请
      wx.showToast({
        title: '您的身份是['+type_text+'],请勿再次申请~',
        icon:'none'
      })
      return ;
    } 

    // 追梦人只能申请成为筑梦者
    if(apply_type=='zmr'&&data_type!=0){
      wx.showToast({
        title: '您的身份是['+type_text+'],请勿再次申请~',
        icon:'none'
      })
      return ;
    }
    wx.navigateTo({
      url: '/pages/apply/'+apply_type
    });
  },
  // 判断手机号是否验证
  checkMobile:function(e){
    if (this.data.alert_mobile) {
      wx.showToast({
        title: '请先认证手机号',
        icon: 'none'
      })
      return;
    }
    console.log(e.currentTarget.dataset)
    // 完成手机认证则跳转
    wx.navigateTo({
      url: e.currentTarget.dataset.url
    })
  },
  onCard:function(){
    wx.navigateTo({
      url: '/pages/h_card/index',
    })
  }
})